var ports = require("../../utils/ports.js")
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    provinceName: '请选择省',
    cityName: '请选择市',
    countyName: '请选择县',
    isDefault: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var title = options.title
    var titleStr = ''
    if (title == 'add') {
      titleStr = '新增地址'
    } else {
      titleStr = '修改地址'
    }
    wx.setNavigationBarTitle({
      title: titleStr,
    })

    var id = options.id
    if (id) {
      this.getOneMemberAddressDetail(id)
    }
    //加载省
    this.loadCity(app.cityRootId, 0)
  },
  changeSwitch(e) {
    var isDefault = e.detail.value
    this.setData({
      isDefault
    })
  },
  delectMemberAddress(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定删除地址吗？',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '提交中',
            icon: 'none',
            mask: true
          })
          var data = {}
          data.memberAddressID = that.data.id
          ports.ModuleMemberAddress.deleteMyMemberAddress(data, that, res => {
            if (res.data.header.code == 0) {
              wx.navigateBack()
            } else {
              wx.showToast({
                title: res.data.header.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  formSubmit: function(e) {
    var that = this
    var data = {}
    var formInfo = e.detail.value
    var contactName = formInfo.contactName
    if (!contactName) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    data.contactName = contactName
    var phone = formInfo.phone
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if(!(/^1[34578]\d{9}$/.test(phone))){
      wx.showToast({
        title: '请输入合法手机号',
        icon: 'none'
      })
      return
    }
    data.phone = phone
    var shengID = that.data.provinceID
    if (!shengID) {
      wx.showToast({
        title: '请选择省',
        icon: 'none'
      })
      return
    }
    data.shengID = shengID
    data.shengName = that.data.provinceName
    var shiID = that.data.cityID
    if (!shiID) {
      wx.showToast({
        title: '请选择市',
        icon: 'none'
      })
      return
    }
    data.shiID = shiID
    data.shiName = that.data.cityName
    var xianID = that.data.countyID
    if (!xianID) {
      wx.showToast({
        title: '请选择县',
        icon: 'none'
      })
      return
    }
    data.xianID = xianID
    data.xianName = that.data.countyName
    var name = formInfo.name
    if (!name) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return
    }
    data.name = name
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      mask: true
    })
    data.shortName = data.shengName + data.shiName + data.xianName
    data.isDefault = that.data.isDefault ? "1" : "0"
    if (that.data.id) {
      that.updateMemberAddress(data)
    } else {
      that.createMemberAddress(data)
    }
  },
  //请选择省
  bindProvincePickerChange: function(e) {
    var that = this;
    var index = parseInt(e.detail.value);
    var provinceList = that.data.provinceList;
    var name = provinceList[index].name;
    var id = provinceList[index].id;
    if (provinceList == null || provinceList.length == 0) {
      return
    }
    that.setData({
      provinceName: name,
      provinceID: id,
      cityName: '请选择市',
      cityID: '',
      cityArray: [],
      countyName: '请选择县',
      countyID: '',
      countyArray: []
    })
    that.loadCity(id, 1)
  },
  //请选择市
  bindCityPickerChange: function(e) {
    var that = this;
    var index = parseInt(e.detail.value);
    var cityList = that.data.cityList;
    if (cityList == null || cityList.length == 0) {
      return
    }
    that.setData({
      cityName: cityList[index].name,
      cityID: cityList[index].id,
      countyName: '请选择县',
      countyID: '',
      countyArray: []
    })
    that.loadCity(that.data.cityList[index].id, 2)
  },
  bindCountyPickerChange: function(e) {
    var that = this;
    var index = parseInt(e.detail.value);
    var countyList = that.data.countyList;
    if (countyList == null || countyList.length == 0) {
      return
    }
    that.setData({
      countyName: countyList[index].name,
      countyID: countyList[index].id,
    })
  },
  loadCity(rootID, index) {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    var req = {};
    req.rootID = rootID;
    req.pageNumber = 999;
    req.depth = 1
    ports.ModuleCity.getCityList(req, that, res => {
      wx.hideToast()
      if (res.data.header.code === 0) {
        var node = res.data.body.data.rows;
        var array = [];
        for (var i = 0; i < node.length; i++) {
          array.push(node[i].name)
        }
        if (index == 0) {
          //省
          that.setData({
            provinceArray: array,
            provinceList: node
          })
        } else if (index == 1) {
          //市
          that.setData({
            cityArray: array,
            cityList: node
          })
        } else if (index == 2) {
          //县
          that.setData({
            countyArray: array,
            countyList: node
          })
        }
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'loading',
          duration: 1000
        })
      }
    })
  },
  //新增地址
  createMemberAddress(data) {
    var that = this
    ports.ModuleMemberAddress.createMemberAddress(data, that, res => {
      if (res.data.header.code == 0) {
        wx.navigateBack()
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  //修改地址
  updateMemberAddress(data) {
    var that = this
    data.memberAddressID = that.data.id
    ports.ModuleMemberAddress.updateMemberAddress(data, that, res => {
      if (res.data.header.code == 0) {
        wx.navigateBack()
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  //修改地址
  updateMemberAddress(data) {
    var that = this
    data.memberAddressID = that.data.id
    ports.ModuleMemberAddress.updateMemberAddress(data, that, res => {
      if (res.data.header.code == 0) {
        wx.navigateBack()
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  getOneMemberAddressDetail(id) {
    var that = this
    var data = {}
    data.memberAddressID = id
    ports.ModuleMemberAddress.getOneMemberAddressDetail(data, that, res => {
      if (res.data.header.code == 0) {
        var memberAddress = res.data.body.data
        that.setData({
          memberAddress,
          id,
          provinceID: memberAddress.shengID,
          cityID: memberAddress.shiID,
          countyID: memberAddress.xianID,
          provinceName: memberAddress.shengName,
          cityName: memberAddress.shiName,
          countyName: memberAddress.xianName,
          isDefault: memberAddress.isDefault == 0 ? false : true
        })
        //加载市数据
        if (memberAddress.shengID) {
          that.loadCity(memberAddress.shengID, 1)
        }
        //加载县数据
        if (memberAddress.shiID) {
          that.loadCity(memberAddress.shiID, 2)
        }
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  }
})